#!/bin/sh
# This script installs a building environment using sbuild as a
# builder for the "gbp buildpackage" command line. The sbuild
# chroot uses an union FS (ie: AUFS in Jessie and Trusty,
# OverlayFS on newer kernels) as this is the fastest backend,
# and that unionFS scratch disk is stored on tmpfs, as this
# is the fastest way to run sbuild.

# Author: Thomas Goirand <zigo@debian.org>
# License: Apache-2.0

set -e
set -x

OSTACK_RELEASE=newton
BUILDER_SYS_USERNAME="jenkins"
BUILDER_HOMEDIR=/home/jenkins
GITREPO_ROOT=${WORKSPACE}

DISTRO_VENDOR=$(lsb_release --id -s | awk '{print tolower($0)}')
HOST_DISTRO_NAME=$(lsb_release -c -s)

####################
# Parse parameters #
####################
# There's 3 OS name concept concepts here:
# HOST_DISTRO_NAME: the os of the host (example: jessie, xenial)
# SBUILD_CHROOT_DISTRO_NAME: the name of the distro to setup in the chroot (example: jessie, xenial)
# SBUILD_CHROOT_NAME: the name of the chroot we want to setup (example: jessie-newton or jessie-newton-backports)
DO_EXIT="no"
for i in $@ ; do
    case "${1}" in
    "--sbuild-chroot-name")
        if [ -z "${2}" ] ; then echo "Parameter for option --sbuild-chroot-name is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
        SBUILD_CHROOT_NAME="${2}"
        shift
        shift
    ;;
    "--sbuild-chroot-distro-name")
        if [ -z "${2}" ] ; then echo "Parameter for option --sbuild-chroot-distro-name is missing" > /dev/stderr ; DO_EXIT="yes" ; fi
        SBUILD_CHROOT_DISTRO_NAME="${2}"
        shift
        shift
    ;;
    esac
done

if [ -z "${SBUILD_CHROOT_NAME}" ] ; then
    SBUILD_CHROOT_NAME="${HOST_DISTRO_NAME}-${OSTACK_RELEASE}"
fi

if [ -z "${SBUILD_CHROOT_DISTRO_NAME}" ] ; then
    SBUILD_CHROOT_DISTRO_NAME=${HOST_DISTRO_NAME}
fi

if [ ${DO_EXIT} = "yes" ] ; then
    echo "Parameters not validated: will exit now!" > /dev/stderr
    exit 1
fi
########################
# End parse parameters #
########################

# Some configuration stuff
CLOSEST_DEBIAN_MIRROR=http://deb.debian.org/debian

PATH=/usr/bin:/usr/sbin:/bin:/sbin
APT="DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends"

detect_distro () {
    DISTRO_VENDOR=$(lsb_release --id -s | awk '{print tolower($0)}')
    DISTRO_NAME=$(lsb_release -c -s)
}

disable_init_start () {
    TMPFILE=$(mktemp)
    echo "#!/bin/sh

exit 101

" >${TMPFILE}
    sudo cp ${TMPFILE} /usr/sbin/policy-rc.d
    sudo chmod +x /usr/sbin/policy-rc.d
    rm ${TMPFILE}
}

enable_init_start () {
    sudo rm -f /usr/sbin/policy-rc.d
}

# This gives the closest mirror in the current cloud where we're building
calculate_nodepool_mirror_host () {
    if ! [ -r /etc/nodepool/provider ] ; then
        echo "Could not read /etc/nodepool/provider"
        exit 1
    else
        . /etc/nodepool/provider
        NODEPOOL_MIRROR_HOST=${NODEPOOL_MIRROR_HOST:-mirror.$NODEPOOL_REGION.$NODEPOOL_CLOUD.openstack.org}
        NODEPOOL_MIRROR_HOST=$(echo $NODEPOOL_MIRROR_HOST|tr '[:upper:]' '[:lower:]')
    fi
}

# This is needed to install the backported version of dpkg
add_bpo_repo_to_host () {
    calculate_nodepool_mirror_host

    echo "deb http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton-backports main
deb-src http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton-backports main
" >/tmp/jessie-newton-backports.list
    sudo cp /tmp/jessie-newton-backports.list /etc/apt/sources.list.d/jessie-newton-backports.list

    # Add the repo key
    echo "-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: GPGTools - https://gpgtools.org

mQENBFgbrv4BCADK/pHoOwW+CVPYSJLjhMIOJ3Ef981pDw2TEL/t0L2L7cNYuYfc
dx3G/kCZ0NBjHAXw6rkkFuGsXG6HDJ1TRKQTtdSqjFtI9/22xaGO6OE20BOQmhVF
m8xnJ4ie9TpebH+y4OZqgKq7E9MByYZkqFWZZwpkmxUO5C+KuSIrZn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eKmZ18k/9uwOKLW/2geey8K5a6XdC0lB8t6XCDz2XD+KVZMge1
xgcKDGptc3xj/hjf+1yHX78yqnK1+DNooBxyvtQZPOOQ3bIrzSsjs0jFJxt6XMF0
seegZtd2V1CM6TmQQs8gdFhiXGN7O9XbdhbxWEflY8HCG6MqwhFkf34YVe9KXy+h
oAC7Ej+rjB0pDzz723tgogtZJaL3dfACaXdO8fCy/3bmvPa/g2Ams8fsPxNNa0To
jtJzV/IDwL4gD13EWuCYnGy6YecLNvkugznb5oWBkwx6Vkhm+C9Y8oEP+3OTGh/j
xZwrZrFZAkLQ6kEKphyVA5imSQ==
=sQCo
-----END PGP PUBLIC KEY BLOCK-----" >/tmp/repo-key.gpg
    sudo apt-key add /tmp/repo-key.gpg
    rm /tmp/repo-key.gpg
    sudo apt-get update
}

install_all_software () {
    local PKGOS_INFRA_BUILD_PKG_PATH
    PKGOS_INFRA_BUILD_PKG_PATH=${GITREPO_ROOT}/build-tools/pkgos-infra-build-pkg

    echo "===> Installing required packages to run sbuild"
    sudo sed -i s/httpredir.debian.org/deb.debian.org/ /etc/apt/sources.list
    sudo apt-get update
    sudo ${APT} git-buildpackage eatmydata build-essential \
        fakeroot pristine-tar ssl-cert sudo debootstrap \
        openstack-pkg-tools lsb-release gnupg haveged

    # Since we're installing openstack-pkg-tools, this may overwrite
    # pkgos-infra-build-pkg if we're building openstack-pkg-tools itself 
    if [ -e ${PKGOS_INFRA_BUILD_PKG_PATH} ] ; then
        sudo cp ${PKGOS_INFRA_BUILD_PKG_PATH} /usr/bin
        sudo chmod +x /usr/bin/pkgos-infra-build-pkg
    fi

    # Jessie needs lintian from jessie-backports, because we are using
    # newer debhelper. New lintian prevents errors like
    # postinst-must-call-ldconfig
    if [ "${HOST_DISTRO_NAME}" = "jessie" ] ; then
        add_bpo_repo_to_host

        # Use lintian from backports
        DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends -t jessie-backports lintian
    # This code thinks anything else than jessie is higher version of lintian
    else
        DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends lintian
    fi

    # Jessie needs sbuild from jessie-backports, because it has the
    # $clean_source = 0 option
    if [ "${HOST_DISTRO_NAME}" = "jessie" ] ; then
        add_bpo_repo_to_host

        # Use sbuild from backports, as we need the $clean_source = 0 option
        DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends -t jessie-backports sbuild

        # Patch libdpkg-perl so that it ignores .gitreview files
        # This is a hack which should be replaced by a newer version of dpkg
        THISDIR=$(pwd)
        cd /usr/share/perl5/Dpkg/Source
        if [ -e /usr/share/openstack-pkg-tools/libdpkg-perl-gitreview.patch ] ; then
            patch -p1 </usr/share/openstack-pkg-tools/libdpkg-perl-gitreview.patch
        else
            patch -p1 <${THISDIR}/misc/libdpkg-perl-gitreview.patch
        fi
        cd ${THISDIR}

	# Patch libsbuild-perl (jessie bpo) so that it doesn't use the --pinentry-mode
	# option of gnupg which isn't available in Jessie
#	THISDIR=$(pwd)
#	cd /usr/share/perl5/Sbuild
#	if [ -e /usr/share/openstack-pkg-tools/libsbuild-perl-ChrootSetup.pm-hotfix.patch ] ; then
#		patch -p1 </usr/share/openstack-pkg-tools/libsbuild-perl-ChrootSetup.pm-hotfix.patch
#	else
#		patch -p1 <${THISDIR}/misc/libsbuild-perl-ChrootSetup.pm-hotfix.patch
#	fi
#	cd ${THISDIR}

    # This code thinks anything else than jessie is higher version of sbuild
    else
        DEBIAN_FRONTEND=noninteractive sudo apt-get install -y --no-install-recommends sbuild
    fi
}

configure_dotgitconfig () {
    git config user.name "OpenStack Package Builder"
    git config user.email "openstack-infra@lists.openstack.org"
}

# This key will be used to sign the built package
gen_package_build_key () {
    echo "===> Generating builder's GPG key to sign packages"
    if ! [ -d ${BUILDER_HOMEDIR}/.gnupg ] ; then
        mkdir -p ${BUILDER_HOMEDIR}/.gnupg
        chmod 755 ${BUILDER_HOMEDIR}/.gnupg
    fi
    mkdir -p ${BUILDER_HOMEDIR}/.gnupg
    chmod 700 ${BUILDER_HOMEDIR}/.gnupg
    chmod 700 ${BUILDER_HOMEDIR}/.gnupg
    if ! [ -r ${BUILDER_HOMEDIR}/.gnupg/gpg.conf ] ; then
        echo "keyserver hkp://pool.sks-keyservers.net
personal-digest-preferences SHA256
default-preference-list SHA512 SHA384 SHA256 SHA224 AES256 AES192 AES CAST5 ZLIB BZIP2 ZIP Uncompressed
cert-digest-algo SHA256
" >${BUILDER_HOMEDIR}/.gnupg/gpg.conf
    fi
    if ! [ -r ${BUILDER_HOMEDIR}/.gnupg/pkgos-gen-key-batchfile ] ; then
        echo "
%echo Generating a basic OpenPGP key
Key-Type: RSA
Key-Length: 4096
Name-Real: Jenkins
Name-Email: ${BUILDER_SYS_USERNAME}@openstack.org
Expire-Date: 0
" >${BUILDER_HOMEDIR}/.gnupg/pkgos-gen-key-batchfile
    fi
    sudo chown -R ${BUILDER_SYS_USERNAME}:${BUILDER_SYS_USERNAME} ${BUILDER_HOMEDIR}/.gnupg
    su ${BUILDER_SYS_USERNAME} -c "gpg --gen-key --batch ${BUILDER_HOMEDIR}/.gnupg/pkgos-gen-key-batchfile"
    gpg --gen-key --batch ${BUILDER_HOMEDIR}/.gnupg/pkgos-gen-key-batchfile
}

configure_sbuild () {
    echo "===> Setting-up sbuild's GPG key"
    # Setup the gpg key for sbuild
    sudo mkdir -p /root/.gnupg
    sudo chmod 600 /root/.gnupg
    sudo gpg --list-keys
    sudo sbuild-update --keygen

    # Add the builder as a sbuild user
    sudo sbuild-adduser ${BUILDER_SYS_USERNAME}

    echo "===> Creating sbuild's schroot chroot using a directory backend (ie: not tarball)"
    # Create the actual schroot env
    if ! [ -e /var/lib/sbuild/${SBUILD_CHROOT_NAME} ] ; then
        sudo sbuild-createchroot ${SBUILD_CHROOT_DISTRO_NAME} /var/lib/sbuild/${SBUILD_CHROOT_DISTRO_NAME} ${CLOSEST_DEBIAN_MIRROR}
    fi
    if ! [ -e /var/lib/sbuild/${DISTRO_NAME} ] ; then
        sudo sbuild-createchroot ${DISTRO_NAME} /var/lib/sbuild/${DISTRO_NAME} ${CLOSEST_DEBIAN_MIRROR}
    fi

    # Let's use a RAMDISK for sbuild chroot to build packages:
    # that's much faster than the HDD backend
    echo "===> Mounting /var/lib/schroot/union/{overlay,underlay} using tmpfs to speed-up build"
    TMPFILE=$(mktemp)
    cp /etc/fstab ${TMPFILE}
    echo "tmpfs           /var/lib/schroot/union/overlay        tmpfs defaults        0       0
tmpfs           /var/lib/schroot/union/underlay        tmpfs defaults        0       0" >>${TMPFILE}
    sudo cp ${TMPFILE} /etc/fstab
    rm ${TMPFILE}
    sudo mount /var/lib/schroot/union/overlay
    sudo mount /var/lib/schroot/union/underlay

    echo "===> Fixing-up newly created schroot's chroot config and /etc/git-buildpackage/gbp.conf"
    # Make sure git-buildpackage is using sbuild
    sudo sed -i 's/^[ #\t]*builder[ #\t]*=.*/builder = sbuild -v --no-apt-update/' /etc/git-buildpackage/gbp.conf
    # Disable clean outside of the chroot (note: this actually doesn't work due to a bug,
    # but let's keep it there still, when we found out how to fix git-buildpackage).
    sudo sed -i 's/^[ #\t]*cleaner[ #\t]*=.*/cleaner = \/bin\/true/' /etc/git-buildpackage/gbp.conf

    # Fix from jessie to jessie-newton
    # or jessie to jessie-newton-backports
    if ! [ "${SBUILD_CHROOT_NAME}" = "${SBUILD_CHROOT_DISTRO_NAME}" ] ; then
        # Fetch the full chroot.d filename
        SBUILD_SRC_NAME=$(ls /etc/schroot/chroot.d/${SBUILD_CHROOT_DISTRO_NAME}-amd64-sbuild-*)
        SBUILD_CHROOTD_FN=$(echo ${SBUILD_SRC_NAME} | sed s/${SBUILD_CHROOT_DISTRO_NAME}/${SBUILD_CHROOT_NAME}/)

        mv ${SBUILD_SRC_NAME} ${SBUILD_CHROOTD_FN}
        sed -i s/${SBUILD_CHROOT_DISTRO_NAME}-amd64-sbuild/${SBUILD_CHROOT_NAME}-amd64-sbuild/ ${SBUILD_CHROOTD_FN}
        mv /var/lib/sbuild/${SBUILD_CHROOT_DISTRO_NAME} /var/lib/sbuild/${SBUILD_CHROOT_NAME}
        sed -i "s#/var/lib/sbuild/${SBUILD_CHROOT_DISTRO_NAME}#/var/lib/sbuild/${SBUILD_CHROOT_NAME}#" ${SBUILD_CHROOTD_FN}
    fi
    # Fetch the full chroot.d filename
    SBUILD_CHROOTD_FN=$(ls /etc/schroot/chroot.d/${DISTRO_NAME}-amd64-sbuild-*)

    # Switch from tarball to directory (ie: aufs) based build
    # (again, that's much faster...)
    sudo sed -i -e "s/^[ \t]*type[ \t]*=.*/type=directory/" ${SBUILD_CHROOTD_FN}
    # Tell that we'd like to use AUFS as backend for union FS
    # note that for Stretch and Xenial, we should switch to overlay instead
    case ${HOST_DISTRO_NAME} in
    jessie|trusty)
        UNION_TYPE=aufs
    ;;
    *)
        UNION_TYPE=overlay
    ;;
    esac
    if ! grep -q union-type ${SBUILD_CHROOTD_FN} ; then
        TMPFILE=$(mktemp)
        cp ${SBUILD_CHROOTD_FN} ${TMPFILE}
        echo "union-type=${UNION_TYPE}" >>${TMPFILE}
        sudo cp ${TMPFILE} ${SBUILD_CHROOTD_FN}
        rm ${TMPFILE}
    fi
    # Just to make sure in case union-type was there already (it's normally not there),
    # we overwrite it.
    sudo sed -i -e "s/^[ \t]*union-type[ \t]*=.*/union-type=${UNION_TYPE}/" ${SBUILD_CHROOTD_FN}

    # Configure ~/.sbuildrc to use our new sbuild chroot
    GPG_KEY_ID=$(su jenkins -c "gpg --list-keys ${BUILDER_SYS_USERNAME}" | grep ^pub | awk '{print $2}' | cut -d/ -f2)
    GPG_KEY_ID=$(gpg --list-keys ${BUILDER_SYS_USERNAME} | grep ^pub | awk '{print $2}' | cut -d/ -f2)
    DOT_SBUILDRC_PATH=${BUILDER_HOMEDIR}/.sbuildrc
    if ! [ -r "${DOT_SBUILDRC_PATH}" ] ; then
        echo "\$build_arch_all = 1;
\$build_source = 1;
\$distribution = '"${SBUILD_CHROOT_NAME}"';
# We may run lintian by hand later on
# not need to get git-buildpackage to do it
\$run_lintian = 0;

# We don't want to run clean of source tree before
# entering the chroot, because that would mean we need
# some of the build dependencies.
\$clean_source = 0;

# Needed for sbuild to show output in deb-auto-backports
\$verbose = 1;

# In case we decide to no sign packages, uncomment this:
#\$pgp_options = '-us -uc';

\$key_id = '${GPG_KEY_ID}';

# Needed for example for python-glance-store, which as in
# build-depends-indep: python3-all (>= 3.5) | python3-enum34
\$resolve_alternatives = 1;

# This is for perl, just leave it...
1;
" >${DOT_SBUILDRC_PATH}
    fi

    # Add our repos to the sbuild chroot
    echo "deb http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton-backports main
deb-src http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton-backports main
" >/tmp/jessie-newton-backports.list
    sudo cp /tmp/jessie-newton-backports.list /var/lib/sbuild/${SBUILD_CHROOT_NAME}/etc/apt/sources.list.d/jessie-newton-backports.list
    echo "deb http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton main
deb-src http://${NODEPOOL_MIRROR_HOST}/debian-openstack jessie-newton main
" >/tmp/jessie-newton.list
    sudo cp /tmp/jessie-newton.list /var/lib/sbuild/${SBUILD_CHROOT_NAME}/etc/apt/sources.list.d/jessie-newton.list
    # Add the repo key
    echo "-----BEGIN PGP PUBLIC KEY BLOCK-----
Comment: GPGTools - https://gpgtools.org

mQENBFgbrv4BCADK/pHoOwW+CVPYSJLjhMIOJ3Ef981pDw2TEL/t0L2L7cNYuYfc
dx3G/kCZ0NBjHAXw6rkkFuGsXG6HDJ1TRKQTtdSqjFtI9/22xaGO6OE20BOQmhVF
m8xnJ4ie9TpebH+y4OZqgKq7E9MByYZkqFWZZwpkmxUO5C+KuSIrZnyDUD+715Kr
kmnpr1VAEP2IUA4dbvp4/DMDK6Ny7cptlxjwQbKTGIXRxMFNIzlQRDS3Ex6eiW2N
kX2nHCn4BBffNB2PhUBsuF3hEEOT2nIj91NUKY8DvGm5m3mgxz1BgUJ4nelTMedy
wXyL8HVUMMjdT8OU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MqwhFkf34YVe9KXy+h
oAC7Ej+rjB0pDzz723tgogtZJaL3dfACaXdO8fCy/3bmvPa/g2Ams8fsPxNNa0To
jtJzV/IDwL4gD13EWuCYnGy6YecLNvkugznb5oWBkwx6Vkhm+C9Y8oEP+3OTGh/j
xZwrZrFZAkLQ6kEKphyVA5imSQ==
=sQCo
-----END PGP PUBLIC KEY BLOCK-----" >/tmp/repo-key.gpg
    sudo cp /tmp/repo-key.gpg /var/lib/sbuild/${SBUILD_CHROOT_NAME}/repo-key.gpg
    sudo chroot /var/lib/sbuild/${SBUILD_CHROOT_NAME} apt-key add /repo-key.gpg
    rm /tmp/repo-key.gpg /var/lib/sbuild/${SBUILD_CHROOT_NAME}/repo-key.gpg
    sudo sbuild-update -udcar ${SBUILD_CHROOT_NAME}
}

detect_distro
disable_init_start
install_all_software
configure_dotgitconfig
gen_package_build_key
configure_sbuild
enable_init_start
